// Definition for singly-linked list.
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode p = l1, q = l2;
        int sum = p.val + q.val, c;
        p.val = sum % 10;
        c = sum /10;
        while (p.next != null & q.next != null) {
            p = p.next;
            q = q.next;
            sum = p.val + q.val + c;
            p.val = sum % 10;
            c = sum /10;
        }
        if (q.next != null)
            p.next = q.next;
        while (p.next != null) {
            p = p.next;
            sum = p.val + c;
            p.val = sum % 10;
            c = sum /10;
        }
        if (c == 1) {
            p.next = new ListNode(1);
        }
        return l1;
    }
}